import 'package:flutter/material.dart';
import 'package:xim/common/get_style_view.dart';
import 'package:xim/config/route_config.dart';
import 'package:xim/http/apis/user/user_model.dart';
import 'package:xim/widget/divider.dart';
import 'create_session_controller.dart';
import 'create_session_style.dart';

class SessionCreatePage extends GetStyleControllerView<List<UserFriendModel>,
    SessionCreatePageController, SessionCreatePageStyle> {
  SessionCreatePage({super.key});

  @override
  Widget buildView(BuildContext context, List<UserFriendModel>? data) {
    return Scaffold(
      appBar: style.appBar,
      backgroundColor: style.bgColor.color,
      body: GestureDetector(
        onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
        child: Column(
          children: [
            style.searchBox(search: (account) async {
              var user = await controller.searchUser(account);
              if(user!=null){
                Routers.toProfilePublicPage(user.account);
              }
            }),
            const SizedBox(height: 6,),
            Column(
              children: [
                style.titleItem(title: "选择一个群",),
                DividerCompose.simpleDivider(horizontalGap: 12, context: context),
                style.titleItem(title: "面对面创建群聊"),
                DividerCompose.simpleDivider(horizontalGap: 12, context: context),
                userList
              ],
            )
          ],
        ),
      ),
    );
  }

  Widget get userList => Column(
        children: controller.statefulUsers
            .map((e) => GestureDetector(
                  onTap: controller.changeUserStatusById(e.userFriendModel.id, !e.isSelected),
                  child: Container(
                    color: Colors.transparent,
                    child: style.item(leftWidget: statefulUserRow(e)),
                  ),
                )
        ).toList(),
  );

  Widget statefulUserRow(StatefulUser user) => Row(
    children: [
      userCheckbox(user),
      style.userAvatar(user.userFriendModel.friendUserInfo.avatar),
      const SizedBox(width: 6,),
      Text(
        user.userFriendModel.friendUserInfo.name,
        style: TextStyle(color: style.textColor.color),
      )
    ],
  );

  Widget userCheckbox(StatefulUser user) => Theme(
        data: ThemeData(unselectedWidgetColor: style.textColor.color),
        child: Checkbox(
          overlayColor: MaterialStateProperty.all(style.textColor.color),
          value: user.isSelected,
          onChanged: (it) {
            controller.changeUserStatusById(user.userFriendModel.id, user.isSelected);
          },
          checkColor: style.textColor.color,
          shape: const CircleBorder(side: BorderSide(width: 2, color: Colors.blue)),
        ),
  );

}
